﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNetNuke.UI.Skins.Controls;
using System.Web;
using System.Web.SessionState;

namespace ShopSpace.UI.DotNetNukeModules
{
	public class UserState
	{
		#region Private Fields

		private string userKey;

		#endregion

		#region Constructor(s)

		public UserState(string userKey)
		{
			this.userKey = userKey;
		}

		#endregion

		#region Private Methods

		private string GetKey(string key)
		{
			return key + "_" + this.userKey;
		}

		#endregion

		#region Private Properties

		private HttpSessionState Session
		{
			get
			{
				return HttpContext.Current.Session;
			}
		}

		private IDictionary<int, IList<ModuleMessage>> Messages
		{
			get
			{
				string key = this.GetKey("Messages");
				if ( Session[key] == null )
				{
					Session[key] = new Dictionary<int, IList<ModuleMessage>>();
				}
				return (IDictionary<int, IList<ModuleMessage>>) Session[key];
			}
		}

		#endregion

		#region Public Properties

		public int SelectedProjectId
		{
			get
			{
				string key = this.GetKey("SelectedProjectId");
				if ( Session[key] != null )
				{
					return (int) Session[key];
				}
				else
				{
					return 0;
				}
			}
			set
			{
				Session[this.GetKey("SelectedProjectId")] = value;
			}
		}

		#endregion

		#region Public Methods

		public IList<ModuleMessage> GetMessages(int moduleId)
		{
			if ( this.Messages.ContainsKey(moduleId) )
			{
				return this.Messages[moduleId];
			}
			return new List<ModuleMessage>();
		}

		public void AddMessage(int moduleId, string text, ModuleMessage.ModuleMessageType type)
		{
			ModuleMessage msg = new ModuleMessage();
			msg.Text = text;
			msg.IconType = type;
			if ( !this.Messages.ContainsKey(moduleId) )
			{
				this.Messages.Add(moduleId, new List<ModuleMessage>());
			}
			this.Messages[moduleId].Add(msg);
		}

		public void ClearMessages(int moduleId)
		{
			if ( this.Messages.ContainsKey(moduleId) )
			{
				this.Messages[moduleId].Clear();
			}
		}

		#endregion
	}
}
